package com.biz.ssm.controller.admin;

import com.biz.ssm.base.Page;
import com.biz.ssm.base.Pageable;
import com.biz.ssm.base.Response;
import com.biz.ssm.domain.Admin;
import com.biz.ssm.domain.AdminRole;
import com.biz.ssm.domain.Role;
import com.biz.ssm.search.RoleSearchBean;
import com.biz.ssm.service.AdminRoleService;
import com.biz.ssm.service.AdminService;
import com.biz.ssm.service.RoleAuthorityService;
import com.biz.ssm.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 角色
 * @ClassName: RoleController
 * @Description: 角色
 * @author tupx
 * @date 2018年11月28日 上午11:26:30
 *
 */
@Controller("adminRoleController")
@RequestMapping("/api/role")
public class RoleController{

	@Resource
	private RoleService roleService;

	@Resource
	private AdminRoleService adminRoleService;

	@Resource
	private AdminService adminService;

	@Resource
	private RoleAuthorityService roleAuthorityService;

	/**
	 * 保存
	 */
	@ApiOperation(value = "保存角色")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAnyRole('admin','roleList')")
	public @ResponseBody
	Response save(@RequestBody Role role) {
		role.setIsSystem(false);
		role.setCreateTime(new Date());
		role.setModifyTime(new Date());
		if (!roleService.createRole(role)) {
			return Response.respFail("创建角色出错了~");
		}
		return Response.respSuccess();
	}

	/**
	 * 编辑
	 */
	@ApiOperation(value = "角色详情")
	@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyRole('admin','roleList')")
	@ResponseBody
	public Response info(@PathVariable("id")Long id) {
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("role", roleService.selectById(id));
		model.put("authorities", roleAuthorityService.getAuthoritysByRole(id));
		return Response.respSuccess(model);
	}

	/**
	 * 更新
	 */
	@ApiOperation(value = "更新角色")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasAnyRole('admin','roleList')")
	public @ResponseBody
	Response update( @RequestBody Role role) {
		Role pRole = roleService.selectById(role.getId());
		if (pRole == null || pRole.getIsSystem()) {
			return Response.respFail("系统管理员不允许操作");
		}
		role.setIsSystem(pRole.getIsSystem());
		role.setModifyTime(new Date());
		roleService.modifyRole(role);
		return Response.respSuccess();
	}

	@ApiOperation(value = "获取角色列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
    @PreAuthorize("hasAnyRole('admin','roleList')")
	public Response<Page<Role>>  list(Pageable pageable, RoleSearchBean searchBean){
		pageable.setBaseSearchBean(searchBean);
		Page<Role> page = roleService.findPage(pageable);
		return Response.respSuccess(page);
	}

	/**
	 * 删除
	 */
	@ApiOperation(value = "删除角色")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
    @PreAuthorize("hasAnyRole('admin','roleList')")
	public Response delete(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				Role role = roleService.selectById(id);
				if (role == null) {
					return Response.respFail("角色不存在，删除出错");
				}
				List<AdminRole> adminRoles = this.adminRoleService.findListByRole(role.getId());
				List<Admin> admins = new ArrayList<Admin>();
				for (AdminRole adminRole : adminRoles) {
					Admin admin = adminService.selectById(adminRole.getAdmins());
					admins.add(admin);
				}
				if (role != null && (role.getIsSystem() || (admins != null && !admins.isEmpty()))) {
					return Response.respFail("角色不存在或为系统管理员，删除出错");
				}
				try {
					roleService.delRole(role);
				} catch (Exception e) {
					return Response.respFail("删除出错");
				}
			}
		}
		return Response.respSuccess();
	}

}
